package com.jwdf.uda.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ActivitySearchBinding;
import com.jwdf.uda.event.SearchInputEvent;
import com.jwdf.uda.ui.fragment.search.SearchDesignerFragment;
import com.jwdf.uda.ui.fragment.search.SearchDesignerResultsFragment;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.UdaUtils;

/**
 * Created by Administrator on 2016/5/19.
 */
public class SearchDesignerActivity extends AbsActivity {
    private ActivitySearchBinding mDataBinding;
    private TextWatcher mTextChangeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mDataBinding.searchEdit.setSelection(s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if(TextUtils.isEmpty(s)) {
                showRecordFragment();
            }
            mDataBinding.ivClearInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
        }
    };
    private TextView.OnEditorActionListener mActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                String content = mDataBinding.searchEdit.getText().toString().trim();
                if(!TextUtils.isEmpty(content)) {
                    UdaUtils.hideInputMethod(v.getContext(), v);
                    mDataBinding.ivClearInput.setVisibility(View.VISIBLE);
                    showResultFragment(content);
                }
                return true;
            }
            return false;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_search);
        initViews();
    }

    @Override
    protected boolean fullScreenMode() {
        return true;
    }

    private void initViews() {
        LinearLayout.LayoutParams flParams = (LinearLayout.LayoutParams) mDataBinding.searchBarLayout.getLayoutParams();
        flParams.height += DisplayUtils.getStatusBarHeight(this);
        mDataBinding.searchBarLayout.setLayoutParams(flParams);
        mDataBinding.searchBarLayout.setPadding(DisplayUtils.dipToPix(this,10), DisplayUtils.getStatusBarHeight(this), DisplayUtils.dipToPix(this,10), 0);

        mDataBinding.searchEdit.setHint(getString(R.string.search_guide_hint));
        mDataBinding.searchEdit.setOnEditorActionListener(mActionListener);
        mDataBinding.searchEdit.addTextChangedListener(mTextChangeListener);
        mDataBinding.ivClearInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDataBinding.searchEdit.setText("");
            }
        });
        showRecordFragment();
    }
    private void showRecordFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_list_container, SearchDesignerFragment.instance(""), null).commitAllowingStateLoss();
    }
    @Override
    protected void onStart() {
        super.onStart();
        EventBusUtil.registerEvent(this);
    }
    @Override
    protected void onStop() {
        super.onStop();
        EventBusUtil.unregisterEvent(this);
    }
    public void onEvent(SearchInputEvent event) {
        mDataBinding.searchEdit.setText(event.content);
        showResultFragment(event.content);
    }
    private void showResultFragment(String content) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_list_container, SearchDesignerResultsFragment.instance(content, PbBean.PbSignSearchType.SEARCH_TYPE_DESIGNER), null).commitAllowingStateLoss();
    }
    public void onCancel(View view) {
        finish();
    }
}

